using System.Security.Claims;
using Microsoft.Owin;
using Microsoft.Owin.Security;
using Microsoft.Owin.Security.Provider;
using Newtonsoft.Json.Linq;

namespace Sharp.Platform.Owin.OAuth.Baidu.Provider
{

    public class BaiduAuthenticatedContext : BaseContext
    {
        public BaiduAuthenticatedContext(IOwinContext context, JObject user, string accessToken)
            : base(context)
        {
            AccessToken = accessToken;
            User = user;

            Userid = TryGetValue(user, "userid");
            UserName = TryGetValue(user, "username");
            RealName = TryGetValue(user, "realname");
        }

        public JObject User { get; private set; }

        public string AccessToken { get; private set; }

        public string Userid { get; private set; }
        public string UserName { get; private set; }
        public string RealName { get; private set; }
        public ClaimsIdentity Identity { get; set; }
        public AuthenticationProperties Properties { get; set; }

        private static string TryGetValue(JObject user, string propertyName)
        {
            JToken value;
            return user.TryGetValue(propertyName, out value) ? value.ToString() : null;
        }
    }
}
